package com.example.registerapi;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author：kangxuan
 * @date：2024/9/30 9:38 AM
 * @desc:
 */
public class RegisterInfo {
    /**
     * 服务名
     */
    private String name;

    /**
     * 服务地址
     */
    private String ip;

    /**
     * 服务端口
     */
    private int port;

    /**
     * 服务状态
     */
    private boolean online;

    /**
     * 失败次数
     */
    private AtomicInteger failCount = new AtomicInteger();

    public RegisterInfo() {
    }

    public int getFailCount() {
        return failCount.get();
    }

    public void addFailCount(int count) {
        this.failCount.addAndGet(count);
    }

    public void resetFailCount() {
        this.failCount.set(0);
    }

    public RegisterInfo(String name, String ip, String port) {
        this.name = name;
        this.ip = ip;
        this.port = Integer.parseInt(port);
        this.online = true;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getKey() {
        return name + ":" + ip + ":" + port;
    }

    public boolean isOnline() {
        return online;
    }

    public void setOnline(boolean online) {
        this.online = online;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        RegisterInfo that = (RegisterInfo) o;
        return port == that.port && name.equals(that.name) && ip.equals(that.ip);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, ip, port);
    }

    @Override
     public String toString() {
         return "RegisterInfo{" +
                 "name='" + name + '\'' +
                 ", ip='" + ip + '\'' +
                 ", port=" + port +
                 ", online=" + online +
                 '}';
     }

    public String getHost() {
        return "http://" + ip + ":" + port;
    }
}
